package com.noteit.login;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.noteit.bean.Account;
import com.noteit.dao.LoginDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStack;

public class GoogleClientLogin extends ActionSupport implements SessionAware,RequestAware{

	private static final long serialVersionUID = 7715720768648239796L;
	private Map<String, Object> sessionMap;
	private Map<String, Object> requestMap;
	private Account acnt;
	private String user; //must be set your gmail
	private String password; //must be set your gmail password
	private String deleteflag;
	private String createdate;
	private String lastlogindate;
	private LoginDAO loginDAO;
	public static String serviceType="cl"; ///ServiceType see more in http://code.google.com/intl/zh-TW/apis/gdata/faq.html#clientlogin
	URL url;
	
	public String initLogin(){
		return "success";
	}
	
	public String googleLoginVerify() throws IOException{
		
		OgnlValueStack stack = (OgnlValueStack) requestMap.get("struts.valueStack");
		HttpServletRequest request = (HttpServletRequest) stack.getContext().get(StrutsStatics.HTTP_REQUEST);
		user = request.getParameter("user");
		password = request.getParameter("password");
		
		System.out.println("user="+user+", password="+password);
		if((user.length()>0 && !user.isEmpty()) && (password.length()>0 && !password.isEmpty())){
			String data = "accountType=HOSTED_OR_GOOGLE"+
			"&Email="+user+
			"&Passwd="+password+
			"&service="+serviceType+
			"&source=Gulp-CalGulp-1.05"; 

			try {
			url = new URL("https://www.google.com/accounts/ClientLogin");
			
			/// Open Connect
			URLConnection conn = url.openConnection(); 
			
			/// setDoOutput=true than can write message to sent request
			conn.setDoOutput(true); 
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(data);
			wr.flush();
			BufferedReader rd = null;
				try {
					/// Get the response
					rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
					String line;
					
					while ((line = rd.readLine()) != null) {
						// Process line...
						System.out.println(line);
					}
					wr.close();
					rd.close();
				} catch (Exception e) {
					System.out.println("err1:"+e.getMessage());
					//e.printStackTrace();
					return "input";
				} finally{
					wr.close();
					//rd.close();
				}
						
			}catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				System.out.println("err2:"+e.getMessage());
				//e.printStackTrace();
				return "input";
			}
			this.saveUserData(user);
			return "success";
		}else{
			return "input";
		}
	}
	
	//save or update user to account db
	public String saveUserData(String user){
		Account acnt = new Account();
		
		return "success";
	}
	
	@Override
	public void setRequest(Map<String, Object> requestMap) {
		this.requestMap = requestMap;
	}
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getDeleteflag() {
		return deleteflag;
	}
	public void setDeleteflag(String deleteflag) {
		this.deleteflag = deleteflag;
	}
	public String getCreatedate() {
		return createdate;
	}
	public void setCreatedate(String createdate) {
		this.createdate = createdate;
	}
	public String getLastlogindate() {
		return lastlogindate;
	}
	public void setLastlogindate(String lastlogindate) {
		this.lastlogindate = lastlogindate;
	}
	
	
}
